/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mrroman.linksender.history;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.LinkedList;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import com.mrroman.linksender.ioc.Init;
import com.mrroman.linksender.ioc.Locales;
import com.mrroman.linksender.ioc.Log;
import com.mrroman.linksender.ioc.Name;
import com.mrroman.linksender.sender.Message;
import com.mrroman.linksender.sender.MessageEvent;
import com.mrroman.linksender.sender.MessageListener;
import javax.swing.JButton;

/**
 *
 * @author gorladam
 */
@Name("history.HistoryKeeper")
public class HistoryKeeper implements MessageListener {

    @Locales
    private ResourceBundle messages;
    @Log
    private Logger logger;
    private final LinkedList<MessageEvent> history = new LinkedList<MessageEvent>();
    public static final String EVENT_NEWMSG = "EVENT_NEWMSG";

    @Init
    public void init() {
    }

    @Override
    public void messageSend(MessageEvent event) {
        if ((event.getMessage().getFlags() & Message.MESSAGE_FLAG_HIDDEN) == 0) {
            synchronized (history) {
                history.add(event);
            }
            propertyChangeSupport.firePropertyChange(EVENT_NEWMSG, null, event);
        }
    }

    @Override
    public void serverInfo(MessageEvent event) {
    }

    public int size() {
        return history.size();
    }

    public MessageEvent get(int index) {
        return history.get(index);
    }

    /**
     * Get the value of history
     *
     * @return the value of history
     */
    public LinkedList<MessageEvent> getHistory() {
        return history;
    }
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
}
